package com.fruit.service.picture.impl;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.ObjectMetadata;
import com.fruit.service.pic.PictureManager;

/**
 * 图片上传服务
 * 
 * @author Administrator
 *
 */
@Service
public class PictureSevice implements PictureManager {

	private final static String endpoint = "oss-cn-beijing.aliyuncs.com";
	private final static String accessKeyId = "LTAIe3jOX1ydPh6D";
	private final static String accessKeySecret = "QnlgTjh9SXYzv47TiD4ZbM0XYvHkGy";
    private final static String bucketName="fruit-picture";

	@Override
	public Map uploadPicture(String path, InputStream in) {
		Map<String, Object> res = new HashMap<String, Object>();
		String url = null;
		OSSClient ossClient = null;
		try {
			ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
			ObjectMetadata meta = new ObjectMetadata();
			meta.setContentType("image/jpeg");
			meta.setCacheControl("no-cache");
			meta.setContentEncoding("utf-8");
		    ossClient.putObject(bucketName, path, in, meta);
			url = "http://"+bucketName+"."+endpoint+"/"+path;
            res.put("error", 0);
			res.put("url", url);
		} catch (Exception e) {
			e.printStackTrace();
			res.put("error", 1);
			res.put("message", "文件上传发生异常");
		} finally {
			ossClient.shutdown();
		}
		return res;
	}

}
